package com.br.flexpront.factory
{
	import com.br.flexpront.controller.EventController;
	import com.br.flexpront.events.LoginEvent;
	import com.br.flexpront.factory.SisRemoteDestination;
	import com.br.flexpront.model.Endereco;
	import com.br.flexpront.model.UserApplication;
	import com.br.flexpront.model.Usuario;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class UserRO
	{

		private static var _userRO:UserRO;
        private var _oRemote:RemoteObject;
		private var eventController : EventController = EventController.getInstance();
		private var aUsuarios : ArrayCollection;
		private var UserApl : UserApplication = UserApplication.getInstance();

		public function UserRO(lock:LockSingleton)
		{
			if (lock == null)
				throw new Error("Use o getInstance()!");

			//criando o objeto remoto
			_oRemote=new RemoteObject();
			_oRemote.destination=SisRemoteDestination.USER_DESTINATION.getDestination();
			_oRemote.source=SisRemoteDestination.USER_DESTINATION.getSource();
			_oRemote.showBusyCursor=true;
			
			//cria o array de usuarios
			aUsuarios = new ArrayCollection();
			
			var usertmp : Usuario;
			var enderecotmp : Endereco;
			
			usertmp = new Usuario();
			usertmp.Codigo = 1;
			usertmp.Login = "sandro";
			usertmp.Senha = "teste";
			usertmp.Nome = "Sandro Marques de Sales"
			usertmp.Cpf = "75418592491";
			usertmp.DataNascimento = new Date("13/10/1972");
			enderecotmp = new Endereco();
			enderecotmp.Codigo=1;
			enderecotmp.Logradouro = "Rua Ananias Catanho";
			enderecotmp.Numero = "192";
			enderecotmp.Uf = "PE";
			enderecotmp.Municipio = "Recife";
			usertmp.endereco = enderecotmp;
			
			aUsuarios.addItem(usertmp);

		}

		static public function getInstance():UserRO
		{
			if (_userRO == null)
				_userRO=new UserRO(new LockSingleton());

			return _userRO;

		}


		public function login(user:Usuario):void
		{
	
			//procura pelo login e senha
			UserApl.user = null;
			for each( var usertmp : Usuario in aUsuarios){
				//usertmp = aUsuarios.getItemIndex(i) as Usuario;
				if( usertmp.Login == user.Login && usertmp.Senha == user.Senha ){
					UserApl.user = usertmp; 			
					break;
				}
			}
			if( UserApl.user != null  ) {

  			   eventController.dispatchEvent(new LoginEvent("LoginEventOk"));
			}else{
				Alert.show('usuario nao encontrado');
				EventDispatcher(LoginEvent.FAIL);
			}
				
			//escuta os eventos
			//_oRemote.ValidarUsuario.addEventListener(ResultEvent.RESULT, loginResult);
			//_oRemote.addEventListener(FaultEvent.FAULT, loginFault);
			//_oRemote.ValidarUsuario(user);

		}

		public function add(user:Usuario):void
		{
		}

		private function loginResult(event:ResultEvent):void
		{

			var user:Usuario = event.result as Usuario;
			//caso o usuario esteja nulo pq nao existe
			if (user == null)
			{
				Alert.show('usuario nao encontrado');
				EventDispatcher(LoginEvent.FAIL);
			}
			else
			{
				UserApplication.getInstance().user=user;
				EventDispatcher(LoginEvent.OK);
			}

		}

		private function loginFault(event:FaultEvent):void
		{
			Alert.show('erro: '+event.fault.toString());
			EventDispatcher(LoginEvent.FAIL);
		}

	}
}

internal class LockSingleton
{
}
;